/**
 * 
 */
package org.swing.utility.input.action;
import java.awt.event.*;
import javax.swing.*;
/**
 * @author Quynh Nhu
 *
 */
public class ActionMapAction extends AbstractAction
{
	private Action originalAction;
	private JComponent component;
	private String actionCommand = "";

	/**
	 *  Replace the default Action for the given KeyStroke with a custom Action
	 *
	 *  @param name       the name parameter of the Action
	 *  @param componet   the component the Action belongs to
	 *  @param actionKey  the key to identify the Action in the ActionMap
	 */
	public ActionMapAction(String name, JComponent component, String actionKey)
	{
		super(name);

		originalAction = component.getActionMap().get(actionKey);

		if (originalAction == null)
		{
			String message = "no Action for action key: " + actionKey;
			throw new IllegalArgumentException(message);
		}

		this.component = component;
	}

	public void setActionCommand(String actionCommand)
	{
		this.actionCommand = actionCommand;
	}

	/**
	 *  Invoke the original Action using the original component as the source
	 *  of the event.
	 */
	public void actionPerformed(ActionEvent e)
	{
		e = new ActionEvent(
			component,
			ActionEvent.ACTION_PERFORMED,
			actionCommand,
			e.getWhen(),
			e.getModifiers());

		originalAction.actionPerformed(e);
	}
}

//JTextField textField = new JTextField();
//Action nextWord = new ActionMapAction(
//    "Select Next Word", textField, "selection-next-word");
//nextWord.putValue(AbstractAction.MNEMONIC_KEY, KeyEvent.VK_N);
//JButton nextButton = new JButton(nextWord);